<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:be="clr-namespace:LogGrok.Infrastructure.Behaviors;assembly=LogGrok.Infrastructure"
                    xmlns:behaviors="clr-namespace:LogGrok.IndexedFilter.Behaviors"
                    xmlns:commands="clr-namespace:LogGrok.IndexedFilter.Commands"
                    xmlns:controls="clr-namespace:LogGrok.Controls;assembly=LogGrok.Controls"
                    xmlns:local="clr-namespace:LogGrok.IndexedFilter"
                    xmlns:metroControls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <DataTemplate x:Key="CategoriesPopup">
        <controls:ResizablePopup Width="250"
                                 Height="300"
                                 MinWidth="200"
                                 MinHeight="200"
                                 StaysOpen="False"
                                 be:BindPopupBehavior.ToToggleButton="{Binding ElementName=CategoriesSearchButton}"
                                 be:RoutedCommandBehavior.RoutedCommandToCommandBinding="{be:RoutedCommandToCommandBinding Command={Binding Source={x:Static commands:PopupCommands.Close}},
                                                                                                                           RoutedCommand={x:Static commands:RoutedCommands.Cancel}}"
                                 behaviors:FocusOnOpenPopupBehavior.DefaultFocusedElement="{Binding ElementName=FilterTextBox}">
            <controls:ResizablePopup.InputBindings>
                <KeyBinding Key="Escape"
                            Command="{x:Static commands:RoutedCommands.Cancel}"
                            CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Popup}}" />
            </controls:ResizablePopup.InputBindings>
            <Border BorderBrush="LightGray" BorderThickness="1">
                <Grid Background="White">
                    <AdornerDecorator>
                        <Grid Margin="4">
                            <Grid.Resources>
                                <CollectionViewSource x:Key="items" Source="{Binding Elements}">
                                    <CollectionViewSource.SortDescriptions>
                                        <scm:SortDescription PropertyName="Category" />
                                    </CollectionViewSource.SortDescriptions>
                                </CollectionViewSource>
                            </Grid.Resources>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBox x:Name="FilterTextBox"
                                     Grid.ColumnSpan="3"
                                     Margin="10, 10, 10, 0"
                                     Text="{Binding TextFilter,
                                                    UpdateSourceTrigger=PropertyChanged}"
                                     be:RoutedCommandBehavior.RoutedCommandToCommandBinding="{be:RoutedCommandToCommandBinding Command={Binding Source={x:Static commands:TextBoxCommands.Clear}},
                                                                                                                               RoutedCommand={x:Static commands:RoutedCommands.Cancel}}"
                                     metroControls:TextBoxHelper.ButtonCommand="{Binding Source={x:Static commands:TextBoxCommands.Clear}}"
                                     metroControls:TextBoxHelper.ButtonCommandParameter="{Binding ElementName=FilterTextBox}"
                                     metroControls:TextBoxHelper.SelectAllOnFocus="True"
                                     metroControls:TextBoxHelper.Watermark="Search...">
                                <TextBox.InputBindings>
                                    <KeyBinding Key="Escape"
                                                Command="{x:Static commands:RoutedCommands.Cancel}"
                                                CommandParameter="{Binding ElementName=FilterTextBox}" />
                                </TextBox.InputBindings>
                                <TextBox.Style>
                                    <Style BasedOn="{StaticResource MetroTextBox}" TargetType="{x:Type TextBox}">
                                        <Setter Property="metroControls:TextBoxHelper.ClearTextButton" Value="True" />
                                        <Style.Triggers>
                                            <Trigger Property="metroControls:TextBoxHelper.HasText" Value="False">
                                                <Setter Property="metroControls:TextBoxHelper.ClearTextButton" Value="False" />
                                            </Trigger>
                                            <Trigger Property="metroControls:TextBoxHelper.HasText" Value="True">
                                                <Setter Property="metroControls:TextBoxHelper.ClearTextButton" Value="True" />
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBox.Style>
                            </TextBox>
                            <Button Grid.Row="1"
                                    Margin="10, 10, 5, 0"
                                    Command="{Binding SelectAll}"
                                    CommandParameter="{Binding Items,
                                                               ElementName=ListBox}"
                                    Content="Select All" />
                            <Button Grid.Row="1"
                                    Grid.Column="1"
                                    Margin="10, 10, 5, 0"
                                    Command="{Binding DeselectAll}"
                                    CommandParameter="{Binding Items,
                                                               ElementName=ListBox}"
                                    Content="Deselect All" />
                            <Button Grid.Row="2"
                                    Grid.ColumnSpan="2"
                                    Margin="10, 10, 5, 0"
                                    Command="{Binding SelectOnlySearchResults}"
                                    CommandParameter="{Binding Items,
                                                               ElementName=ListBox}"
                                    Content="Select Only Search Results" />
                            <ListBox x:Name="ListBox"
                                     Grid.Row="3"
                                     Grid.ColumnSpan="4"
                                     Margin="10"
                                     HorizontalContentAlignment="Stretch"
                                     ItemsSource="{Binding Source={StaticResource items}}"
                                     SelectionMode="Single"
                                     behaviors:ItemsFilteringBehavior.FilteredProperty="Name"
                                     behaviors:ItemsFilteringBehavior.TextFilter="{Binding TextFilter}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate DataType="{x:Type local:ElementViewModel}">
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <CheckBox Command="{Binding Path=DataContext.RefreshActiveChanged,
                                                                        ElementName=ListBox}"
                                                      Content="{Binding Name}"
                                                      IsChecked="{Binding IsActive}" />
                                            <TextBlock Grid.Column="1"
                                                       Margin="0,0,5,0"
                                                       HorizontalAlignment="Right"
                                                       FontStyle="Italic"
                                                       Foreground="Gray"
                                                       Text="{Binding Percent}" />
                                        </Grid>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </Grid>
                    </AdornerDecorator>
                </Grid>
            </Border>
        </controls:ResizablePopup>
    </DataTemplate>


</ResourceDictionary>